﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager.Customer
{
    public partial class Csim : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                BindDdlCustomer(ref ddlCustomer);
                ddlCustomer.Items.Insert(0, new ListItem("全部", "-1"));

                BindDdlODEP(ref ddlODEP);
                ddlODEP.Items.Insert(0, new ListItem("全部", "-1"));

                GVDataBind();
            }
        }

        #region GV Bind
        private void GVDataBind()
        {
            string strSQL = "SELECT distinct a.CNO,a.IMSI,b.CityName,c.ProvinceName,d.OName FROM T_SIM a,T_Area b,T_Province c,T_ODEP d WHERE a.AreaNO=b.AreaNO and a.ONO=b.ONO and b.ProvinceID=c.ProvinceID and a.ONO=d.ONO ";

            strSQL += string.Format(" AND a.CNO IN (SELECT CNO FROM T_User_Customer WHERE UserID='{0}')", userModel.UserID);
            string strProvinceID = MM.DBUtility.BLSqlData.GetProvinceIDbyQuickName(txtProvince.TextValue);
            if (!string.IsNullOrEmpty(strProvinceID))
                strSQL += string.Format(" AND c.ProvinceID={0}", strProvinceID);
            if (!string.IsNullOrEmpty(txtIMSI.Text.Trim()))
                strSQL += string.Format(" AND a.IMSI LIKE '%{0}%'", txtIMSI.Text.Trim());
            if (!"-1".Equals(ddlCustomer.SelectedValue))
                strSQL += string.Format(" AND a.CNO='{0}'", ddlCustomer.SelectedValue);
            if (!"-1".Equals(ddlODEP.SelectedValue))
                strSQL += string.Format(" AND a.ONO='{0}'", ddlODEP.SelectedValue);

            if (ddlOrder.SelectedValue.Equals("ProvinceName"))
                strSQL += string.Format(" ORDER BY c.{0} {1}", ddlOrder.SelectedValue, ddlAD.SelectedValue);
            else
                strSQL += string.Format(" ORDER BY a.{0} {1}", ddlOrder.SelectedValue, ddlAD.SelectedValue);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;

        }
        private void GVDataBindAll()
        {
            string strSQL = "SELECT distinct a.CNO,a.IMSI,b.CityName,c.ProvinceName,d.OName FROM T_SIM a,T_Area b,T_Province c,T_ODEP d WHERE a.AreaNO=b.AreaNO and a.ONO=b.ONO and b.ProvinceID=c.ProvinceID and a.ONO=d.ONO ";

            strSQL += string.Format(" AND a.CNO IN (SELECT CNO FROM T_User_Customer WHERE UserID='{0}')", userModel.UserID);
            if (ddlOrder.SelectedValue.Equals("ProvinceID"))
                strSQL += string.Format(" ORDER BY c.{0} {1}", ddlOrder.SelectedValue, ddlAD.SelectedValue);
            else
                strSQL += string.Format(" ORDER BY a.{0} {1}", ddlOrder.SelectedValue, ddlAD.SelectedValue);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;

        }
        protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVinfo.PageIndex = e.NewPageIndex;
            GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
            GVinfo.DataBind();
        }
        #endregion

        private void BindDdlCustomer(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query(string.Format("SELECT a.CNO,a.CNO+'-'+ b.CName as Name FROM T_User_Customer a,T_Customers b WHERE a.CNO=b.CNO AND a.UserID='{0}'", userModel.UserID)).Tables[0];
            ddl.DataValueField = "CNO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        private void BindDdlODEP(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT ONO,ONO+'-'+ OName as Name FROM T_ODEP ").Tables[0];
            ddl.DataValueField = "ONO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
